home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: 16 Feb 96 20:19:36 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.824501976@rscernix>
- References: <11f7cc$17261a.3b3@daprez> <4g1vl1$rm2@maverick.tad.eds.com>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4g1vl1$rm2@maverick.tad.eds.com> Ed McGuffey <fgae23@ods04.and.ifg.gmeds.com> writes:
- ^^^^^^^^
- I have misspelled McGoofey ;-) Sorry for the tasteless joke, couldn't
- resist.
-
- >If you really want to use the !, try:
- >
- > if (!((strcmp(argv[1], "-d") || (strcmp(argv[1], "-e")))
- > Usage();
- >
- >since you want the ! to apply to the entire expression consisting of the
- >two compares. The code is more readable, however, to avoid negative logic
- >altogether and just say:
- >
- > if ((strcmp(argv[1], "-d") || (strcmp(argv[1], "-e"))
-
- Your parentheses don't match. Why? :-)
-
- > function();
- > else
- > Usage();
-
- Oh no! There is no way to stop people from posting their own broken
- versions of the original broken version of the code :-(
-
- The above one will ALWAYS call function(), because it is IMPOSSIBLE
- that BOTH strcmp(argv[1], "-d") AND (strcmp(argv[1], "-e") return 0.
-
- Don't play smart and suppress the == or != operators from conditional
- expressions, even if they _can_ be suppressed. If you use them, the
- chances of getting it right are considerably increased, e.g.
-
- if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "-e") == 0)
- function();
- else
- Usage();
-
- is considerably more readable than the equivalent:
-
- if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "-e"))
- ...
-
- or the De Morgan'ized version of the above:
-
- if (!(strcmp(argv[1], "-d") && strcmp(argv[1], "-e")))
- ...
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-